<?php
/**
 * Created by PhpStorm.
 * User: Lin
 * Date: 2017/4/8 0008
 * Time: 下午 16:40
 */
class RbacAction extends CommonAction {

    //用户列表
    public function userList(){
        $userArr = M('user')->select();
        $this->userArr=$userArr;
        $this->display();
    }

    //添加用户
    public function addUser(){
        if($id = $_GET['id']){
            $this->userInfo=M('user')->find($id);
        }
        $this->display();
    }

    //添加用户处理
    public function runAddUser(){
        $id = $_POST['id'];
        $db = M('user');
        $msg=$id?'更新':'添加';
        if($id) {
            $data=array(
                'id'=>$_POST['id'],
                'username'=>$_POST['username'],
                'status'=>$_POST['status'],
                'password'=>md5($_POST['password']),
            );
            //更新
            if($db->save($data)) {
                $this->success($msg.'成功',U('userList'));
            } else {
                $this->error($msg.'失败',U('userList'));
            }
        }else {
            $data=array(
                'username'=>$_POST['username'],
                'password'=>md5($_POST['password']),
            );
            //保存
            if($db->add($data)) {
                $this->success($msg.'成功',U('userList'));
            } else {
                $this->success($msg.'失败',U('userList'));
            }
        }
    }


    //删除用户
    public function runDeleteUser(){
        $id = I('get.id',0,'int');
        if( M('user')->delete($id) ) {
            $this->success('删除成功',U('userList'));
        } else {
            $this->error('删除失败');
        }
    }

    //节点列表
    public function nodeList(){
        $nodeArr = M('node')->select();
        $this->nodeArr = node_merge($nodeArr);
//        p(node_merge($nodeArr),1);
        $this->display();
    }

    //添加及编辑节点
    public function addNode(){
        //添加表单默认情况
        $this->info = array(
            'level' => I('get.level',1,'int'),
            'pid' => I('get.pid',0,'int'),
            'status' => 0,
            'sort' => 50
        );
        switch ($this->info['level']){
            case 1: {
                $this->label = "应用";
                break;
            }
            case 2: {
                $this->label = "控制器";
                break;
            }
            case 3: {
                $this->label = "方法";
                break;
            }
        }
        if($_GET['id']) {
            //编辑模式
            $this->info = M('node')->where(array('id'=>$_GET['id']))->find();
        }
        $this->display();
    }

    //添加及编辑节点表单处理
    public function runAddNode(){
        $id = $_POST['id'];
        $db = M('node');
        $msg=$id?'更新':'添加';
        if($id) {
            //更新
            if($db->save($_POST)) {
                $this->success($msg.'成功',U('nodeList'));
            } else {
                $this->error($msg.'失败',U('nodeList'));
            }
        }else {
            //保存
            if($db->add($_POST)) {
                $this->success($msg.'成功',U('nodeList'));
            } else {
                $this->success($msg.'失败',U('nodeList'));
            }
        }
    }

    //删除节点
    public function runDeleteNode(){
        $id = I('get.id','0','int');
        $db = M('node');
        $data = $db->where(array('pid'=>$id))->field('id')->find();
        if($data) {
            $this->error('该节点存在子节点，删除失败',U('nodeList'));
        } else {
            if($db->delete($id)) {
                $this->success('删除成功',U('nodeList'));
            } else {
                $this->error('删除失败，请稍后重试',U('nodeList'));
            }
        }
        //if($data['level'] === 3)
    }

    //角色列表
    public function roleList(){
        $this->roleArr = M('role')->select();
        $this->display();
    }

    //添加及编辑角色.
    public function addRole(){
        if($_GET['id']) {
            $id = I('get.id',0,'int');
            $this->role = M('role')->find($id);
        }
        $this->display();
    }

    //添加角色表单处理
    public function runAddRole(){
        $id = $_POST['id'];
        $db = M('role');
        $msg=$id?'更新':'添加';
        if($id) {
            //更新
            if($db->save($_POST)) {
                $this->success($msg.'成功',U('roleList'));
            } else {
                $this->error($msg.'失败',U('roleList'));
            }
        }else {
            //保存
            if($db->add($_POST)) {
                $this->success($msg.'成功',U('roleList'));
            } else {
                $this->success($msg.'失败',U('roleList'));
            }
        }
    }

    //删除角色
    public function runDeleteRole(){
        $id = I('get.id',0,'int');
        if( M('role')->delete($id) ) {
            $this->success('删除成功',U('roleList'));
        } else {
            $this->error('删除失败');
        }
    }

     //给角色添加节点权限
    public function access(){
        $id = I('id',0 ,'intval');
        $node = M('node')->where("status = 1")->field(array('id','title','pid','name','level'))->select();

        //获取原有权限
        $access = M('access')->where("role_id = $id")->getField('node_id',true);
        $this->id=$id;
        $this->nodeArr = node_merge($node,$access);
        $this->display();
    }

    //添加节点权限表单处理
    public function setAccess(){
        $rid = I('id', '', 'intval');
        $db = M('access');
        //清空原有权限
        $db->where("role_id = $rid")->delete();

        //插入新的权限
        $data = array();

        foreach ($_POST['access'] as $v) {
            $tmp = explode('_', $v);
            $data[] = array(
                'role_id'=> $rid,
                'node_id'=> $tmp[0],
                'level'=>$tmp[1]
            );
        }
        if($db->addAll($data)) {
            $this->success('修改成功',U('roleList'));
        } else {
            $this->success('修改失败');
        }
    }
    
    //给用户添加角色
    public function relationRole(){
        $id = I('id',0 ,'intval');
        $roleArr = M('role')->where("status = 1")->field(array('id','name','remark'))->select();

        //获取原有角色
        $relationRole = M('role_user')->where("user_id = $id")->getField('role_id',true);
        $this->id=$id;
        $this->roleArr = role_judge($roleArr,$relationRole);
        $this->display();
    }

    //添加角色处理
    public function runRelationRole(){
        $id = I('id', '', 'intval');
        $db = M('role_user');
        //清空原有权限
        $db->where("user_id = $id")->delete();

        //插入新的权限
        $data = array();

        foreach ($_POST['role_id'] as $v) {
            $data[] = array(
                'role_id'=> $v,
                'user_id'=> $id,
            );
        }
        if($db->addAll($data)) {
            $this->success('修改成功',U('userList'));
        } else {
            $this->success('修改失败');
        }
    }
}